import { defineStore } from 'pinia'
import * as logApi from '../../../api/system/log'
import type { OperationLog } from '../../../types/app.d'

// 日志管理 Store 接口
interface LogManagementState {
  operationLogList: OperationLog[]
  total: number
  loading: boolean
  currentLog: OperationLog | null
}

export const useLogStore = defineStore('logManagement', {
  state: (): LogManagementState => ({
    operationLogList: [],
    total: 0,
    loading: false,
    currentLog: null
  }),

  getters: {
    // 获取操作日志列表
    getOperationLogList(): OperationLog[] {
      return this.operationLogList
    },

    // 获取操作日志总数
    getOperationLogTotal(): number {
      return this.total
    },

    // 获取加载状态
    getLoading(): boolean {
      return this.loading
    },

    // 获取当前操作日志
    getCurrentLog(): OperationLog | null {
      return this.currentLog
    }
  },

  actions: {
    // 获取操作日志列表
    async getOperationLogListAction(params: {
      page: number
      pageSize: number
      username?: string
      operation?: string
      status?: number
      beginTime?: string
      endTime?: string
      ip?: string
    }): Promise<void> {
      this.loading = true
      try {
        const response = await logApi.getOperationLogList(params)
        if (response.code === 200) {
          this.operationLogList = response.data.list || []
          this.total = response.data.total || 0
        }
      } catch (error) {
        console.error('获取操作日志列表失败:', error)
      } finally {
        this.loading = false
      }
    },

    // 获取操作日志详情
    async getOperationLogInfoAction(id: string): Promise<void> {
      try {
        const response = await logApi.getOperationLogInfo(id)
        if (response.code === 200) {
          this.currentLog = response.data
        }
      } catch (error) {
        console.error('获取操作日志详情失败:', error)
      }
    },

    // 删除操作日志
    async deleteOperationLogAction(id: string): Promise<boolean> {
      try {
        const response = await logApi.deleteOperationLog(id)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('删除操作日志失败:', error)
        return false
      }
    },

    // 批量删除操作日志
    async batchDeleteOperationLogAction(ids: string[]): Promise<boolean> {
      try {
        const response = await logApi.batchDeleteOperationLog(ids)
        if (response.code === 200) {
          return true
        }
        return false
      } catch (error) {
        console.error('批量删除操作日志失败:', error)
        return false
      }
    },

    // 清空操作日志
    async cleanOperationLogAction(): Promise<boolean> {
      try {
        const response = await logApi.cleanOperationLog()
        if (response.code === 200) {
          this.operationLogList = []
          this.total = 0
          return true
        }
        return false
      } catch (error) {
        console.error('清空操作日志失败:', error)
        return false
      }
    },

    // 导出操作日志
    async exportOperationLogAction(params?: {
      username?: string
      operation?: string
      status?: number
      beginTime?: string
      endTime?: string
      ip?: string
    }): Promise<string> {
      try {
        const response = await logApi.exportOperationLog(params)
        if (response.code === 200) {
          return response.data || ''
        }
        return ''
      } catch (error) {
        console.error('导出操作日志失败:', error)
        return ''
      }
    },

    // 清空当前日志
    clearCurrentLog(): void {
      this.currentLog = null
    }
  }
})